package com.hoopchina.util.xml;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.hoopchina.meta.NewsItem;
import com.hoopchina.meta.NewsSearchResultItem;


import android.util.Log;
import android.util.Xml;

public class NewsItemXML {
    private static final String XMLPULL_INFO = "XmlPull_Info";

    public static List<NewsItem> getNewsList(InputStream ainputstream) {
    	List<NewsItem>  mNewsResultList = null;
        NewsItem mCurrentNewsItem = null;
        InputStream in =null;
        XmlPullParser mXmlPull = Xml.newPullParser();
     // 将GBK转为utf-8       
      BufferedReader bf;
		try {
			bf = new BufferedReader(new InputStreamReader(ainputstream,"gb2312"));
	           StringBuffer buffer=new StringBuffer();  
	            String line="";  
	            while((line=bf.readLine())!=null){  
	                buffer.append(line);  
	            }  
	              
	        String buff = buffer.toString();  
	        in=new ByteArrayInputStream(buff.getBytes());  
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	try {

	    /**
	     * 提供的xml内容方式都是以输入流的方式InputStream
	     **/
	    mXmlPull.setInput(in, "UTF-8");
	    /**
	     * 触发事件getEventType() =>返回事件码 当它遇到某个字符,如果符合xml的语法规范. 它就会出发这个语法所代表的数字
	     * Returns the type of the current event (START_TAG, END_TAG, TEXT,
	     * etc.)
	     **/
	    int eventCode = mXmlPull.getEventType();

	    /**
	     * 解析事件: StartDocument,文档开始 Enddocument,文档结束
	     * 
	     * 每次读到一个字符,就产生一个事件 只要解析xml文档事件不为空,就一直往下读
	     **/
	    while (eventCode != XmlPullParser.END_DOCUMENT) {
           

    		switch (eventCode) {
    		case XmlPullParser.START_DOCUMENT: // 文档开始事件,可以做一些数据初始化处理
    			mNewsResultList =  new ArrayList<NewsItem>();
    		    break;
    
    		case XmlPullParser.START_TAG:// 元素开始.
    			String  name = mXmlPull.getName();
                if (name.equalsIgnoreCase("item")) {
    		        mCurrentNewsItem = new NewsSearchResultItem();
    		    } 
                else  if (mCurrentNewsItem != null){
	                if (name.equalsIgnoreCase("title")) {
	    		        mCurrentNewsItem.setTitle( mXmlPull.nextText().trim() );
	    		    } else if (name.equalsIgnoreCase("link")) {
	    		        mCurrentNewsItem.setUrl( mXmlPull.nextText().trim() );
	                } else if (name.equalsIgnoreCase("description")) {
	                    mCurrentNewsItem.setAbs( mXmlPull.nextText().trim() );
	                } else if (name.equalsIgnoreCase("author")) {
	                    mCurrentNewsItem.setAuthor( mXmlPull.nextText().trim() );
	                }  
	                else if (name.equalsIgnoreCase("pubData")) {
	                    mCurrentNewsItem.setTime( mXmlPull.nextText());
	                }  else if (name.equalsIgnoreCase("publictime")) {
	                    mCurrentNewsItem.setmPublicTime( mXmlPull.nextText().trim() );
	                }else if (name.equalsIgnoreCase("imgUrl")) {
	                    mCurrentNewsItem.setImgUrl( mXmlPull.nextText().trim() );
	                } else { 
	                    // throw
	                }
                }
    		    break;
    
    		case XmlPullParser.END_TAG: // 元素结束,
    
    		    if (mCurrentNewsItem != null
    			    && mXmlPull.getName().equalsIgnoreCase("item")) {
    		    	mNewsResultList.add(mCurrentNewsItem);
    		        mCurrentNewsItem = null;
    		    }
    
    		    break;
    		default :
    		    break;
    		}
		eventCode = mXmlPull.next();// 进入到一下一个元素. 
	    }
	} catch (XmlPullParserException e) {
	    Log.i(XMLPULL_INFO, e.toString());
	} catch (IOException e) {
	    Log.i(XMLPULL_INFO, e.toString());
	} catch (Exception e) {
	    Log.i(XMLPULL_INFO, e.toString());
	}
	return mNewsResultList;
    }

}